package com.slz3.business.module.memberManagement.memAppKey.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.memberManagement.memAppKey.domain.MemAppKey;
import com.slz3.business.module.memberManagement.memAppKey.domain.MemAppKeyFactory;
import com.slz3.business.module.memberManagement.memAppKey.steward.MemAppKeySpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Slf4j
@Service
public class MemAppKeyServiceImpl implements MemAppKeyService {
    @Override
    public MemAppKey save(MemAppKey memAppKey) {
        return Repositorys.memAppKey.save(MemAppKeyFactory.initBaseFields(memAppKey));
    }
    @Override
    public Page<MemAppKey> page(MultiValueMap<String, String> params) {
        Page<MemAppKey> memAppKeyPage = Repositorys.memAppKey.findAll(
                MemAppKeySpecification.findJoin(params), PageableSteward.extract(params)
        );
        return memAppKeyPage;
    }


    @Override
    public MemAppKey modify(MemAppKey memAppKeyNew) {
        return Repositorys.memAppKey.saveAndFlush(MemAppKeyFactory.initBaseFields(memAppKeyNew));
    }

    @Override
    public Optional<MemAppKey> single(UUID uuid) {
        return Repositorys.memAppKey.findById(uuid);
    }

    @Override
    public List<MemAppKey> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.memAppKey.findAllById(uuids);
    }

    @Override
    public List<MemAppKey> delete(List<MemAppKey> memAppKeyList) {
        memAppKeyList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            MemAppKeyFactory.initBaseFields(item);
        });
        return Services.memAppKeyTransactional.batchModify(memAppKeyList);
    }

}
